<?php

namespace Home\Controller;

/**
 * 不用登录的公开对外页面
 * @modify AmosZhong 2016-01-07 创建
 */
class PushCenterPageController extends \Home\Controller\ParentController {

    protected function _initialize() {
        parent::_initialize();
        $this->validateUtil = new \Org\Util\Validate();
    }

    public function index() {

		$param = I('param.');
		$validateRule = array(
            'hotel' => 'notnull,notempty',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $userId = getUserId();
        $room = empty($param['room']) ? null : $param['room'];

        
        if (isset($param['hotel'])) {
            $hotelId = $param['hotel'];
        } else {
            // 获取用户管理客栈id 并且客栈开启状态
           /* $AdminHotelModel = D('AdminHotel');
            $adminHotelInfo = $AdminHotelModel->getInfoByUserLeftHotelEntity($userId);
            $hotelId = array_column($adminHotelInfo, 'hotelentity_id');*/
        }

        // 房型和房间号
        $roomTypeLogic = A('Common/RoomType', 'Logic');
        $roomInfo = $roomTypeLogic->getInfoByHotel($hotelId, true);
        // 判断是否需要根据房间权限
        //$adminId = getUserId();
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');
        /*foreach ($hotelId as $key => $userHotelId) {
            if (!$adminAuthorityLogic->checkIsAdmin($userId, $userHotelId)) {
                $accountLogic = A('Common/Account', 'Logic');
                $roomAuthority = $accountLogic->getAdminRoomAuthority($userId, $userHotelId);
                foreach ($roomInfo as $index => &$roomTypeItem) {
                    foreach ($roomTypeItem['room'] as $roomIndex => $roomItem) {
                        if (!empty($roomAuthority) && !in_array($roomItem['id'], $roomAuthority)) {
                            unset($roomTypeItem['room'][$roomIndex]);
                        }
                    }
                    unset($roomItem);

                    if (empty($roomTypeItem['room'])) {
                        unset($roomInfo[$index]);
                    } else {
                        $roomTypeItem['room'] = array_values($roomTypeItem['room']);
                    }
                }
                unset($roomTypeItem);
            }
        }
        unset($userHotelId);*/

        $roomsCount = 0;
        if (!empty($roomInfo)) {
            foreach ($roomInfo as $roomType) {
                if (!empty($roomType['room'])) {
                    $roomsCount += count($roomType['room']);
                }
            }
        }
        $this->assign('roomsCount', $roomsCount);
        $this->assign('rooms', $roomInfo);
        $this->assign('filterRoomId', $room);
        //钟点房
        $hourRooms =D('HourRoom')->getInfosJoinName($hotelId);
        $this->assign('hourRooms', $hourRooms);
        //长租房
        $longRooms =D('LongRoom')->getInfosJoinName($hotelId);
        $this->assign('longRooms', $longRooms);

        $pageIndex = empty($param['p']) ? 1 : intval($param['p']);
        $pageIndex = $pageIndex > 0 ? $pageIndex : 1;
        $pageSize = C('NEWS_PAGE_SIZE');
        $keys = isset($param['keys']) && !empty($param['keys']) ? $param['keys'] : null;
        $type = isset($param['type']) ? $param['type'] : null;

        $newsPushLogic = A('Common/NewsPush', 'Logic');
        $newsPushInfo = $newsPushLogic->getNewsList($hotelId, $userId, $pageIndex, $pageSize, $keys, $type);
        
        //设置link
        $link = "pushNews_###." . C('URL_HTML_SUFFIX');
        // 检查链接参数
        if ($hotelId || $keys) {
            $link = !is_array($hotelId) ? $link . "?hotel=" . $hotelId : $link;
            $link = $keys ? $link . "&keys=" . $keys : $link;
        }

        $pageUtil = classpage($newsPushInfo['page']['total'],  $link, $pageSize);
        $pageUtil->setConfig('size', 'small');
        $pageFooter = $pageUtil->show();

        // 其他消费项目数据
        $hotelLogic = A('Common/Hotel', 'Logic');
        $otherCostData = $hotelLogic->getHotelOtherCost($hotelId);
        $this->assign('otherCost', $otherCostData);

        $hotelPayData = $hotelLogic->getHotelPay($hotelId, false, null, true);
        $this->assign('hotelpay', $hotelPayData);
        $noCardhotelPayData = $hotelLogic->getHotelPay($hotelId, false);
        $this->assign('noCardhotelPayData', $noCardhotelPayData);

        // 记一笔项目数据
        $hotelNoteData = $hotelLogic->getHotelNote($hotelId, false, null, true);
        $this->assign('noteInCome', $hotelNoteData['income']);
        $this->assign('noteOutCome', $hotelNoteData['outcome']);

        $hotelChannelData = $hotelLogic->getHotelChannel($hotelId, false);
        $this->assign('hotelchannel', $hotelChannelData);

        // 获取系统货币
        $currencyLogic = A('Common/Currency', 'Logic');
        $currencyData = $currencyLogic->getBpCurrency($hotelId);
        $currencyDefaultData = my_sort($currencyData, 'type', SORT_DESC)[0];
        $this->assign('currencyInfo', $currencyData);
        $this->assign('currencyDefaultInfo', $currencyDefaultData);

        $this->assign('newsPushInfo', $newsPushInfo['newsList']);
        $this->assign('pageFooter', $pageFooter);
        $this->assign('pageInfo', $newsPushInfo['page']);

        $this->display();
    }
    
    public function warn() {

        $param = I('param.');

        $userId = getUserId();
        $hotelLogic = A('Common/Hotel', 'Logic');
        $pageIndex = empty($param['p']) ? 1 : intval($param['p']);
        $pageIndex = $pageIndex > 0 ? $pageIndex : 1;
        $pageSize = C('NEWS_PAGE_SIZE');
        $keys = isset($param['keys']) && !empty($param['keys']) ? $param['keys'] : null;

        if (isset($param['hotel'])) {
            $hotelId = $param['hotel'];
        } else {
            $AdminHotelModel = D('AdminHotel');
            $adminHotelInfo = $AdminHotelModel->getInfoByUserLeftHotelEntity($userId);
            $hotelId = array_column($adminHotelInfo, 'hotelentity_id');
        }

        $remindList = $hotelLogic->getRemindList($hotelId, $pageIndex, $pageSize, $keys, 1);
        if (empty($remindList)) {
            $remindList['page']['total'] = 0;
            $remindList['page']['pageTotal'] = 0;
        }
        //设置link
        $link = "pushWarn_###." . C('URL_HTML_SUFFIX');
        // 检查链接参数
        if ($hotelId || $keys) {
            $link = !is_array($hotelId) ? $link . "?hotel=" . $hotelId : $link;
            $link = $keys ? $link . "&keys=" . $keys : $link;
        }

        $pageUtil = classpage($remindList['page']['total'],  $link, $pageSize);
        $pageUtil->setConfig('size', 'small');
        $pageFooter = $pageUtil->show();

        $this->assign('remindList', $remindList['remindList']);
        $this->assign('pageFooter', $pageFooter);
        $this->assign('pageInfo', $remindList['page']);

        $this->display();
    }

    public function check() {

        $param = I('param.');
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);

        $userId = getUserId();
        $room = empty($param['room']) ? null : $param['room'];
        $hotelId = $param['hotel'];

        // 房型和房间号
        $roomTypeLogic = A('Common/RoomType', 'Logic');
        $roomInfo = $roomTypeLogic->getInfoByHotel($hotelId, true);
 
        $roomsCount = 0;
        if (!empty($roomInfo)) {
            foreach ($roomInfo as $roomType) {
                if (!empty($roomType['room'])) {
                    $roomsCount += count($roomType['room']);
                }
            }
        }

        $this->assign('roomsCount', $roomsCount);
        $this->assign('rooms', $roomInfo);
        $this->assign('filterRoomId', $room);
        //钟点房
        $hourRooms =D('HourRoom')->getInfosJoinName($hotelId);
        $this->assign('hourRooms', $hourRooms);
        //长租房
        $longRooms =D('LongRoom')->getInfosJoinName($hotelId);
        $this->assign('longRooms', $longRooms);

        $pageIndex = empty($param['p']) ? 1 : intval($param['p']);
        $pageIndex = $pageIndex > 0 ? $pageIndex : 1;
        $pageSize = C('NEWS_PAGE_SIZE');
        $keys = isset($param['keys']) && !empty($param['keys']) ? $param['keys'] : null;

        $newsPushLogic = A('Common/NewsPush', 'Logic');
        $roomCheckInfo = $newsPushLogic->getCheckInfo($hotelId, $keys, $pageIndex, $pageSize, [1,2]);
        //设置link
        $link = "pushCheck_###." . C('URL_HTML_SUFFIX');
        // 检查链接参数
        if ($hotelId || $keys) {
            $link = !is_array($hotelId) ? $link . "?hotel=" . $hotelId : $link;
            $link = $keys ? $link . "&keys=" . $keys : $link;
        }

        $pageUtil = classpage($roomCheckInfo['page']['total'],  $link, $pageSize);
        $pageUtil->setConfig('size', 'small');
        $pageFooter = $pageUtil->show();

        // 其他消费项目数据
        $hotelLogic = A('Common/Hotel', 'Logic');
        $otherCostData = $hotelLogic->getHotelOtherCost($hotelId);
        $this->assign('otherCost', $otherCostData);

        $hotelPayData = $hotelLogic->getHotelPay($hotelId, false, null, true);
        $this->assign('hotelpay', $hotelPayData);
        $noCardhotelPayData = $hotelLogic->getHotelPay($hotelId, false);
        $this->assign('noCardhotelPayData', $noCardhotelPayData);

        // 记一笔项目数据
        $hotelNoteData = $hotelLogic->getHotelNote($hotelId, false, null, true);
        $this->assign('noteInCome', $hotelNoteData['income']);
        $this->assign('noteOutCome', $hotelNoteData['outcome']);

        $hotelChannelData = $hotelLogic->getHotelChannel($hotelId, false);
        $this->assign('hotelchannel', $hotelChannelData);

        // 获取系统货币
        $currencyLogic = A('Common/Currency', 'Logic');
        $currencyData = $currencyLogic->getBpCurrency($hotelId);
        $currencyDefaultData = my_sort($currencyData, 'type', SORT_DESC)[0];

        $this->assign('currencyInfo', $currencyData);
        $this->assign('currencyDefaultInfo', $currencyDefaultData);

        $this->assign('roomCheckInfo', $roomCheckInfo['roomCheck']);
        $this->assign('pageFooter', $pageFooter);
        $this->assign('pageInfo', $roomCheckInfo['page']);

        $this->display();
    }
}
